package cn.ydt.ydt.user.webapi.controller;

import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.pojo.user.dto.UserRegisterDTO;
import cn.ydt.ydt.pojo.user.dto.UserUpdateDTO;
import cn.ydt.ydt.pojo.user.model.User;
import cn.ydt.ydt.pojo.user.vo.UserShowInfoVO;
import cn.ydt.ydt.user.service.IUserService;
import cn.ydt.ydt.user.service.IVolunteerService;
import cn.ydt.ydt.user.webapi.mapper.UserMapper;
import cn.ydt.ydt.user.webapi.utils.SecurityContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    IUserService userService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IVolunteerService iVolunteerService;

    @PostMapping("/reg")
    @ApiOperation("用户注册")
    public JsonResult reg( @Validated @RequestBody UserRegisterDTO userRegisterDTO) throws ParseException {
        userService.reg(userRegisterDTO);
        return JsonResult.ok("注册成功啦！");
    }

    @GetMapping("/query/{userId}")
    @ApiOperation("用户查询个人信息")
    public JsonResult findByUserId(@PathVariable @RequestBody Long userId){
        UserShowInfoVO userShowInfoVO = userMapper.findUserById(userId);
        userService.findUserById(userId);
        return JsonResult.ok(userShowInfoVO,"查询成功");
    }

    @GetMapping("/findByphone{userPhone}")
    @ApiOperation("根据电话查个人信息")
    public JsonResult findByUserPhone( String userPhone){
        UserRegisterDTO userRegisterDTO = userMapper.findByUserPhone(userPhone);
        userService.findByUserPhone(userPhone);
        return JsonResult.ok(userRegisterDTO,"查询成功");
    }


    @PostMapping("/update")
    @ApiOperation("修改更新个人信息")
    public JsonResult update(@Validated @RequestBody UserUpdateDTO userUpdateDTO) throws ParseException {
        userService.update(userUpdateDTO);
        return JsonResult.ok("修改成功");
    }

    @GetMapping("/getUser")
    @ApiOperation("获取当前登录用户的信息")
    public JsonResult<UserShowInfoVO> getLoginUser(){
        UserShowInfoVO userShowInfoVO = userService.getUserByUserId();
        return JsonResult.ok(userShowInfoVO);
    }

    @GetMapping("/getUser/{communityId}")
    @ApiOperation("获取当前当前社区登录用户的信息")
    public JsonResult<UserShowInfoVO> getLoginUserToCommunityId(@PathVariable Long communityId){
        UserShowInfoVO userShowInfoVO = userService.getUserByUserIdToCommunityId(communityId);
        return JsonResult.ok(userShowInfoVO);
    }

    @GetMapping("/getVolunteer")
    @ApiOperation("获取志愿者信息")
    public JsonResult<List<User>> getVolunteerInfo(){
        List<User> user = iVolunteerService.volunteerInfo();
        return JsonResult.ok(user);
    }

    @GetMapping("/getApplyVolunteer")
    @ApiOperation("获取志愿者信息")
    public JsonResult<List<User>> getVolunteerApplyInfo(){
        List<User> user = iVolunteerService.volunteerApplyInfo();
        return JsonResult.ok(user);
    }

    @GetMapping("/selectAll")
    @ApiOperation("查询所有用户信息")
    public JsonResult<List> selectByIsVolunteer(){
        List<User> user = userService.selectByIsVolunteer();
        userService.selectByIsVolunteer();
        return JsonResult.ok(user,"查看成功");
    }
    @GetMapping("/isDeleted/{isDeleted}/{userId}")
    @ApiOperation("启用禁用/删除个人信息")
    public JsonResult isDeleted(@PathVariable Boolean isDeleted , @PathVariable Long userId) {
        userService.isDeleted(isDeleted,userId);
        return JsonResult.ok("成功");
    }

    @PostMapping("/isVolunteerApplication")
    @ApiOperation("申请成为志愿者")
    public JsonResult isVolunteerApplication(){
        iVolunteerService.isVolunteerApplication();
        return JsonResult.ok("成功");
    }
    @PostMapping("/isVolunteerSet{userId}")
    @ApiOperation("审核成为志愿者")
    public JsonResult isVolunteerSet(@PathVariable Long userId){
        iVolunteerService.isVolunteerSetAccess(userId);
        return JsonResult.ok("成功");
    }
    @PostMapping("/isVolunteerFail{userId}")
    @ApiOperation("审核失败")
    public JsonResult isVolunteerFail(@PathVariable Long userId){
        iVolunteerService.isVolunteerSetFail(userId);
        return JsonResult.ok("成功");
    }
    @PostMapping("/updateIcon")
    @ApiOperation("添加用户上传的图片到数据库")
    public JsonResult updateIcon(@RequestBody String userIcon){
        log.info("输出图片名字{}",userIcon);
        userService.updateIcon(userIcon);
        return JsonResult.ok("成功");
    }
    @GetMapping("/refillingIcon")
    @ApiOperation("将图片返回给前端页面")
    public JsonResult refillingIcon( ){
        String img = userService.refillingIcon();
        return JsonResult.ok(img,"成功");
    }

}
